package com.student.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.student.VO.PageInfoResult;
import com.student.VO.UserVO;
import com.student.VO.productPageVO;
import com.student.dto.ResponseDto;
import com.student.mapper.UserMapper;
import com.student.pojo.SnackAddress;
import com.student.pojo.User;
import com.student.service.UserService;
import com.student.util.PageResultUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @BelongsProject: makeUp
 * @BelongsPackage: com.student.service.impl
 * @Author: 小徐
 * @CreateTime: 2024-12-09 21:31
 * @Description: TODO
 * @Version: 1.0
 */
@Service
public class UserServiceImpl implements UserService {
    @Autowired
    private UserMapper userMapper;

    @Override
    public ResponseDto userLogin(UserVO phUserVO) {
        User user = userMapper.userLogin(phUserVO);
        if (user != null) {
            return ResponseDto.success(user);
        }
        return ResponseDto.error(null);
    }

    @Override
    public ResponseDto userRegister(UserVO phUserVO) {
//        这边是查询是否有账号在
        User user = userMapper.selectUserLogin(phUserVO);
        System.err.println(user);
        if (user == null) {
            Integer res=userMapper.userRegister(phUserVO);
            if (res>=1) {
                return ResponseDto.success(null);
            }
        }
        return ResponseDto.error(null);
    }

    @Override
    public ResponseDto updateImage(Integer userId, String image) {
        Integer  res =userMapper.updateImage(userId,image);
        if (res >= 1) {
            return ResponseDto.success(null);
        }
        return ResponseDto.error(null);
    }
//
    @Override
    public ResponseDto getAllAddress(productPageVO productPageVO) {
        //        mybatis自带的分页插件，按照名字做排序
        PageHelper.startPage(productPageVO.getCurrentPage(), productPageVO.getPageSize());
        List<SnackAddress> snackGoodsCates = userMapper.getAllAddress(productPageVO.getId());

        PageInfo<SnackAddress> cyCardPageInfo = new PageInfo<>(snackGoodsCates);

        PageInfoResult result = PageResultUtils.getResult(cyCardPageInfo);

        System.out.println(result);
        return ResponseDto.success(result);
    }

    @Override
    public ResponseDto updateAddress(SnackAddress snackAddress) {
        Integer  res =userMapper.updateAddress(snackAddress);
        if (res >= 1) {
            return ResponseDto.success(null);
        }
        return ResponseDto.error(null);
    }

    @Override
    public ResponseDto deleteAddress(Integer id) {
        Integer  res =userMapper.deleteAddress(id);
        if (res >= 1) {
            return ResponseDto.success(null);
        }
        return ResponseDto.error(null);
    }

    @Override
    public ResponseDto addAddress(SnackAddress snackAddress) {
        Integer  res =userMapper.addAddress(snackAddress);
        if (res >= 1) {
            return ResponseDto.success(null);
        }
        return ResponseDto.error(null);
    }

    @Override
    public ResponseDto getAlluserMessage(Integer id) {
        User user=userMapper.getAlluserMessage(id);
        if (user!=null){
            return ResponseDto.success(user);
        }
        return ResponseDto.error(null);
    }

    @Override
    public ResponseDto recharge(Integer id, double money) {
        Integer res=userMapper.recharge(id,money);
        if (res>0){
            return ResponseDto.success(null);
        }
        return ResponseDto.error(null);
    }
}